//1. 引入路由和Vue
import Vue from "vue";
import VueRouter from "vue-router";

//2. 引入路由组件
import Home from "../pages/Home";
import Login from "../pages/Login";
import News from "../pages/Home/News";
import Play from "../pages/Home/Play";
import Music from "../pages/Home/Music";

//2. 使用VueRouter这个插件
Vue.use(VueRouter);

//重写push
//保留原始push
const oldPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (
  location,
  onComplete = () => {},
  onAbort = () => {}
) {
  oldPush.call(this, location, onComplete, onAbort);
};

//重写replace和重写push一致

//3. 创建路由对象
const router = new VueRouter({
  mode: "history",
  //路由表配置
  routes: [
    //初始路由重定向
    {
      path: "/",
      redirect: "/home",
    },
    {
      path: "/home",
      component: Home,
      // name: "home",
      //设置默认子路由-方式2
      // redirect: "/home/news",
      children: [
        //设置默认子路由-方式1
        {
          path: "",
          redirect: "news",
        },
        {
          path: "news",
          component: News,
          name: "news",
        },
        {
          path: "play",
          component: Play,
          name: "play",
        },
        {
          path: "music",
          component: Music,
          name: "music",
        },
      ],
    },
    {
      path: "/Login",
      component: Login,
      name: "login",
    },
  ],
});

//4. 把创建好的路由对象暴露出去
export default router;
